#!/bin/env python
# -*- coding: UTF-8 -*-
import sys
import subprocess
from proxyprovider import getActiveProxy
import re

def info():
    return "Parse html for freeproxy.ru ."

def parse(pagedata):
    #print pagedata
    pat = re.compile(r"^\d+\s+(?P<host>[^:]+):(?P<port>\d+)$")
    for line in pagedata.splitlines():
        mat = pat.match(line)
        if mat is not None:
            try:
                yield mat.group("host"), int(mat.group("port")), 'http'
            except:
                continue



def getPage(pageUrl, needProxy):
    #print pageUrl, needProxy
    proxystr = getActiveProxy(protocol = 'http')
    #wgetstr = "http_proxy=http://%s wget %s -O -" % (proxystr, pageUrl)
    #wgetstr = "http_proxy=http://%s wget %s -O - 2>&1" % (proxystr, pageUrl)
    wgetstr = "wget %s -O - 2>&1" % pageUrl
    if needProxy:
        wgetstr = ("http_proxy=http://%s " % proxystr) + wgetstr
    #print wgetstr
    try:
        wgetout = subprocess.check_output(wgetstr, shell = True)
    except subprocess.CalledProcessError:
        return None
    #return "shit "  + str(len(wgetout))
    return wgetout


if __name__ == "__main__":
    getPage("http://www.freeproxy.ru/download/lists/goodproxy.txt", True)
